package com.hmall.api.config;

import com.hmall.api.fallback.ItemClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class DefaultFeignConfig {

    @Bean
    public Logger.Level level(){
        return Logger.Level.NONE;
    }

    /**
     * OpenFeign调用有没有校验
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor(){
        return template -> {
            Long user = UserContext.getUser();
            log.info("在发起OpenFeign调用之前，从ThreadLocal上获取用户ID：{}", user);
            if(user == null){
                return;
            }
            template.header("user-info", user.toString());
        };
    }

    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }

}
